/**
 * 套餐管理初始化
 */
var VpnAdsManage = {
    id: "VpnAdsManageTable",	//表格id
    seItem: null,		//选中的条目
    table: null,
    layerIndex: -1
};

/**
 * 初始化表格的列
 */
VpnAdsManage.initColumn = function () {
    return [[
            {title: '广告名称', field: 'adName', align: 'center'},
            {title: '广告位置', field: 'adLocation', align: 'center',
                templet:function (res) {
                    if (1 == res.adLocation){
                        return "首页Banner"
                    }else if (2 == res.adLocation){
                        return "启动页"
                    }else if (3 == res.adLocation){
                        return "首页全屏弹窗"
                    }
                }},
            {title: '生效时间', field: 'startTime', align: 'center',width:200},
            {title: '失效时间', field: 'endTime', align: 'center',width:200},
            {title: '总观看量', field: 'pvCount', align: 'center'},
            {title: '用户观看量', field: 'uvCount', align: 'center'},
            {title: '点击量', field: 'clickCount', align: 'center'},
            {title: '转化率', field: 'clickCount', align: 'center', templet:function (res) {
                if (res.uvCount == "" || res.uvCount == 0){
                    return "0%"
                }else{
                    var value = res.clickCount / res.uvCount * 100;
                    if(value > 0){
                        value =  value.toFixed(2);
                    }
                    return value + "%";
                }
                }},
            {title: '展示人群', field: 'showType', align: 'center', templet:function (res) {
                    return res.showType.replace("0","免费")
                        .replace("1","VIP")
                        .replace("2","专属线路")
                }},
            {title: '广告状态', field: 'status', align: 'center', templet:function (res) {
                    if (res.status == 0){
                        return "未上线"
                    }else if (res.status == 1){
                        return "已上线"
                    }else if (res.status == 2){
                        return "已下线"
                    }
                }},
            {title: '操作', field: 'adId',
                align: 'center',width: 120, templet:function (res) {
                    var options =  "<div>";
                    if (res.status == 0){
                        options = options + "<a style='color: #06ffe6' onclick=\"onLine("+res.adId+")\">上线</a>";
                        options = options +"<a style='color: #1E9FFF;margin-left: 20px;' onclick=\"VpnAdsManage.updateAds("+res.adId+")\">修改</a>" +
                            "</div>";
                    }else if (res.status == 1){
                        options = options + "<a style='color: #ff2822'onclick=\"offLine("+res.adId+")\">下线</a>" + "</div>";
                    }else if (res.status == 2){
                        options = options + "<a style='color: #1E9FFF'onclick=\"VpnAdsManage.updateAds("+res.adId+")\">修改</a>" + "</div>";
                    }
                    return options;
                }}
    ]];
};

VpnAdsManage.updateAds = function (adId) {
    var index = layer.open({
        type: 2,
        title: '修改广告信息',
        area: ['800px', '600px'], //宽高
        fix: false, //不固定
        maxmin: true,
        content: Feng.ctxPath + '/vpnAdsManage/vpnAdsManage_update/' + adId
    });
    this.layerIndex = index;
};

/**
 * 上线
 */
function onLine (adId) {
    layer.confirm("确定上线此广告？",{icon:3,title:'提示'},
        function (index) {
            var ajax = new $ax(Feng.ctxPath + "/vpnAdsManage/changeAdsStatus", function (data) {
                console.log(data);
                if (data.code == 200){
                    Feng.success("上线成功!");
                    VpnAdsManage.refresh();
                }else{
                    Feng.error(data.message);
                }
            }, function (data) {
                Feng.error(data.responseJSON.message);
            });
            ajax.set("adId",adId);
            ajax.set("status","1");
            ajax.start();
            layer.close(index)
        },function (index) {
            layer.close(index)
        })
};


function offLine (adId) {
    layer.confirm("确定下线此广告？",{icon:3,title:'提示'},
        function (index) {
            var ajax = new $ax(Feng.ctxPath + "/vpnAdsManage/changeAdsStatus", function (data) {
                Feng.success("下线成功!");
                VpnAdsManage.refresh();
            }, function (data) {
                Feng.error(data.responseJSON.message);
            });
            ajax.set("adId",adId);
            ajax.set("status","2");
            ajax.start();
            layer.close(index)
        },function (index) {
            layer.close(index)
        })
};


/**
 * 刷新列表
 */
VpnAdsManage.refresh = function(){
    VpnAdsManage.table.reload("tableReload",{
        page: {
            curr: 1 //重新从第1页开始
        }
    });
};

/**
 * 搜索
 */
VpnAdsManage.search = function(){
    var queryData =  {};

    queryData['adName']= $("#adName").val();
    queryData["adLocation"] = $("#adLocation").val();
    queryData["startTime"] = $("#startTime").val();
    queryData["endTime"] = $("#endTime").val();
    queryData["status"] = $("#adStatus").val();

    var showType = "";

    $("input[name='showType']:checked").each(function (i) {
        showType = showType + $(this).val() + ",";
    });
    showType = showType.substring(0,showType.length-1);

    queryData["showType"] = showType;
    VpnAdsManage.tableList(queryData);
};
/**
 * 导出表格
 */
VpnAdsManage.export = function(){
    var adName = $("#adName").val();
    var adLocation = $("#adLocation").val();
    var startTime = $("#startTime").val();
    var endTime = $("#endTime").val();
    var status = $("#adStatus").val();

    var showType = "";

    $("input[name='showType']:checked").each(function (i) {
        showType = showType + $(this).val() + ",";
    });
    showType = showType.substring(0,showType.length-1);

    var hrefUrl =
        Feng.ctxPath + "/vpnAdsManage/export?" +
        "adName="+adName+"&adLocation="+adLocation
        +"&startTime="+startTime+"&endTime="+endTime
        +"&status="+status+"&showType="+showType;
    window.location.href = hrefUrl;
};



VpnAdsManage.tableList = function(queryData){
    layui.use("table",function () {
        var table = layui.table;

        VpnAdsManage.table = table;

        table.render({
            elem: "#table",
            url: Feng.ctxPath +"/vpnAdsManage/list",
            cellMinWidth: 120,
            where: queryData,
            page: true,
            limit: 20,
            cols: VpnAdsManage.initColumn(),
            parseData: function(res){
                return{
                    "code":0,
                    "msg":"获取成功",
                    "count":res.total,
                    "data": res.items
                }
            },
            text: {
                none: '暂无相关数据'
            },
            id: "tableReload"
        });
    });
};

$(function () {
    VpnAdsManage.tableList();
});
